// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import api from './service'
import * as utils from './utils'
import { sync } from 'vuex-router-sync'

import { Card, Loading } from '@/components/Common'
Vue.component('Card', Card)
Vue.component('Loading', Loading)

import 'nprogress/nprogress.css'

sync(store, router)

// 表单验证
import VeeValidate, { Validator } from 'vee-validate'
import messages from '@/assets/js/zh_CN.js'
Validator.updateDictionary({
  zh_CN: {
    messages
  }
})
const config = {
  errorBagName: 'errors', // change if property conflicts.
  delay: 0,
  locale: 'zh_CN',
  messages: null,
  strict: true
}
Vue.use(VeeValidate, config)

Vue.config.productionTip = false

/* 饿了么组件 */
import { Notification, TimePicker, Select, Option, DatePicker, MessageBox } from 'element-ui'
Vue.component(TimePicker.name, TimePicker)
Vue.component(Select.name, Select)
Vue.component(Option.name, Option)
Vue.component(DatePicker.name, DatePicker)

Vue.prototype.$dateFormat = utils.dateFormat
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$api = api
Vue.prototype.$openMessage = ({message = '', type = 'error', title = '温馨提示'}) => {
  Notification({title, message, type, duration: 1500})
}
/* 前端鉴权 */
router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    if (store.state.global.token === '') {
      next()
    } else {
      if (undefined !== from.path) {
        next(from.path)
      } else {
        next('/')
      }
    }
  } else {
    if (store.state.global.token === '') {
      next('/login')
    } else {
      next()
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
